package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.subject.Mapper.SubjectMapper;
import com.admin.education.dataTransferObject.subject.SubjectTransfer;
import com.admin.education.dataTransferObject.userInformation.Mapper.UserInformationMapper;
import com.admin.education.dataTransferObject.userInformation.Request.QueryUserInformationRequest;
import com.admin.education.dataTransferObject.userInformation.UserInformationTransfer;
import com.admin.education.models.QUserInformation;
import com.admin.education.models.Subjects;
import com.admin.education.models.UserInformation;
import com.admin.education.repositories.UserInformationRepository;
import com.admin.education.services.Interfaces.IUserInformationService;
import com.querydsl.jpa.impl.JPAQuery;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.List;

@Service
@Transactional
@Validated
public class UserInformationService implements IUserInformationService {
    @Autowired
    private UserInformationMapper userInformationMapper;
    private QUserInformation qUserInformation;
    @Autowired
    private UserInformationRepository userInformationRepository;

    public UserInformationService() {
        qUserInformation = QUserInformation.userInformation;
    }

    @Override
    public ServicesExcuteResult<PageResponse<UserInformationTransfer>> queryUserInformation(@Valid QueryUserInformationRequest queryUserInformationRequest) {
        ServicesExcuteResult<PageResponse<UserInformationTransfer>> servicesExcuteResult = new ServicesExcuteResult<>();

        JPAQuery<UserInformation> userInformationJPAQuery = this.userInformationRepository.selectFrom(qUserInformation);
        if (!StringUtils.isEmpty(queryUserInformationRequest.getName())) {
            userInformationJPAQuery = userInformationJPAQuery.where(qUserInformation.name.eq(queryUserInformationRequest.getName()));
        }
        long totalCount = userInformationJPAQuery.fetchCount();

        userInformationJPAQuery = userInformationJPAQuery.orderBy(qUserInformation.id.desc())
                .offset((queryUserInformationRequest.getPageIndex() - 1) * queryUserInformationRequest.getPageSize()).limit(queryUserInformationRequest.getPageSize());

        List<UserInformation> userInformations = userInformationJPAQuery.fetch();

        List<UserInformationTransfer> userInformationTransfers =userInformationMapper.mapFrom(userInformations);

        PageResponse<UserInformationTransfer> subjectTransferPageResponse = new PageResponse<>(queryUserInformationRequest.getPageSize(), queryUserInformationRequest.getPageIndex(), (int) totalCount, userInformationTransfers);

        servicesExcuteResult.setData(subjectTransferPageResponse);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult auditUserInformation(long userInformationId, boolean pass) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<UserInformation> userInformationJPAQuery = this.userInformationRepository.selectFrom(qUserInformation).where(qUserInformation.id.eq(userInformationId));
        if (0 != userInformationJPAQuery.fetchCount()) {
            UserInformation userInformation = userInformationJPAQuery.fetchOne();
            if (!UserInformation.UserInformationStatus.WaitAudit.equals(userInformation.getStatus())) {
                servicesExcuteResult.setErrorMessage("不能继续审批");
            } else {
                if (pass)
                    userInformation.setStatus(UserInformation.UserInformationStatus.Normal);
                else
                    userInformation.setStatus(UserInformation.UserInformationStatus.NoPass);
                this.userInformationRepository.merge(userInformation);

                servicesExcuteResult.setSuccess(true);
            }
        } else {
            throw new ResourceNotFoundException("不存在该用户信息");
        }
        return servicesExcuteResult;
    }
}
